import axios from "axios";
import type { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance } from "axios";
import { ElMessage } from 'element-plus'
interface Result  {
    code:number;
    msg:string
}
interface ResultData<T = any> extends Result{
    data?:T
}
enum RequestEnum {
    SUCCESS=200,
    FAIL=203,
    OVERDUE=501
}
const config = {
    baseURL: import.meta.env.VITE_APP_BASE_URL, // 设置API的基础URL
    timeout:20000
}
class Instance {
    service:AxiosInstance;
    public constructor(config:AxiosRequestConfig){
        this.service = axios.create(config);
        // 请求拦截器
        this.service.interceptors.request.use(
            (config: AxiosRequestConfig): any => {
                // 可在请求发送前对config进行修改，如添加请求头等
                const Authorization = localStorage.getItem("Authorization")||''
                return{
                    ...config,
                    headers:{
                        Authorization:Authorization
                    }
                }
            },
            (error: AxiosError) => {
                // 处理请求错误
                return Promise.reject(error);
            }
        );
        // 响应拦截器
        this.service.interceptors.response.use(
            (response: AxiosResponse) => {
                // 对响应数据进行处理
                const {data,config} = response
                // 登录信息失效时清空本地Authorization
                if(data.code==RequestEnum.OVERDUE){
                    localStorage.setItem("Authorization",'')
                    localStorage.setItem("UserId",'')
                }
                if(data.code&&data.code!==RequestEnum.SUCCESS){
                    // 报错
                    ElMessage.error(data.msg)
                    return Promise.reject(data)
                }
                return data;
            },
            (error: AxiosError) => {
                const {response} = error
                if(response){
                    this.handleCode(response.status)
                }
                if(!window.navigator.onLine){
                    ElMessage.error("网络连接失败")
                }
                // 处理响应错误
                return Promise.reject(error);
            }
        );
    }
    handleCode(code:number){
        switch(code){
            case 500:
                ElMessage.error('登录失败')
                break
            default:
                // 提示错误
                ElMessage.error('请求失败')
                break
        }
    }
     // 常用方法封装
    get<T>(url: string, params?: object): Promise<ResultData<T>> {
        return this.service.get(url, {params});
    }
    post<T>(url: string, params?: object): Promise<ResultData<T>> {
        return this.service.post(url, params);
    }
    put<T>(url: string, params?: object): Promise<ResultData<T>> {
        return this.service.put(url, params);
    }
    delete<T>(url: string, params?: object): Promise<ResultData<T>> {
        return this.service.delete(url, {params});
    }
}

export default new Instance(config).service;
